//  : Declaration of the CV5WrWorks

#pragma once
#include "resource.h"       // main symbols

#include <xercesc/dom/DOMDocument.hpp>

class CBitmapHandler;

#define ID_SLDWORKS_EVENTS 0

#define ParaSolid

//IDL SECTION
// IV5WrWorks
[
	object,
	uuid("4958BBB2-F420-46DE-8228-AFB0474A4422"),
	dual,	
	helpstring("IV5WrWorks Interface"),
	pointer_default(unique)
]
__interface IV5WrWorks : IDispatch
{
	[id(3), helpstring("method ToolbarCallback0")] HRESULT ToolbarCallback0();
	[id(4), helpstring("method ToolbarEnable0")] HRESULT ToolbarEnable0([out, retval] long* status);
	[id(5), helpstring("method SaveV5Part")] HRESULT SaveV5Part([in] BSTR FileName);
	[id(6), helpstring("method SaveV5Product")] HRESULT SaveV5Product([in] BSTR FileName);
#ifdef ParaSolid
	[id(7), helpstring("method SaveV5PartPS")] HRESULT SaveV5PartPS([in] BSTR FileName);
	[id(8), helpstring("method SaveV5ProductPS")] HRESULT SaveV5ProductPS([in] BSTR FileName);
#endif
};


//C++ HEADER SECTION
// CV5WrWorks

[
	coclass,
	threading(apartment),
	vi_progid("V5WrWorks.V5WrWorks"),
	progid("V5WrWorks.V5WrWorks.1"),
	version(1.0),
	uuid("A1B94E65-8A8F-4F1F-A794-099FDC4938A7"),
	helpstring("V5WrWorks Class")
]
class ATL_NO_VTABLE CV5WrWorks : 
	public IV5WrWorks,
	public ISwAddin,
	public IDispEventImpl<ID_SLDWORKS_EVENTS, CV5WrWorks, &__uuidof(DSldWorksEvents), &LIBID_SldWorks, ID_SLDWORKS_TLB_MAJOR, ID_SLDWORKS_TLB_MINOR>
{
	typedef IDispEventImpl<ID_SLDWORKS_EVENTS, CV5WrWorks, &__uuidof(DSldWorksEvents), &LIBID_SldWorks, ID_SLDWORKS_TLB_MAJOR, ID_SLDWORKS_TLB_MINOR> CSldWorksEvents;

private:
	CComPtr<ISldWorks> iSwApp;
	CComPtr<ICommandManager> iCmdMgr;
	long addinID;
	long toolbarID;
	WORD m_swMajNum;
	WORD m_swMinNum;

public:
	CV5WrWorks()
	{
	}

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease() 
	{
	}

	//Utility Methods
public:
	CComPtr<ISldWorks> GetSldWorksPtr() { return iSwApp != NULL ? iSwApp : NULL; }
	//These methods will connect and disconnect this addin to the SolidWorks events
	VARIANT_BOOL AttachEventHandlers();
	VARIANT_BOOL DetachEventHandlers();

	int GetSldWorksTlbMajor() {return (m_wMajorVerNum >= ID_SLDWORKS_TLB_MAJOR ) ? m_wMajorVerNum : 0;}
	int GetSldWorksTlbMinor() {return m_swMinNum;}

	
public:
	//UI Creation
	//These methods control the User Interface for this addin
	void AddCommandManager();
	void RemoveCommandManager();

	void AddFileSaveItems();
	void RemoveFileSaveItems();

	void CreateDocuments( IComponent2* piComponent, xercesc::DOMDocument* pDoc, int nDepth );
// 	void ConvertPartFile( BSTR FileName );
// 	void BuildAssembly( IAssemblyDoc* pAssemblyDoc, int nSize, AHAsmTreeNode* pTreeNode, int nDepth = 0 );
	void ConvertPartFile( BSTR FileName );

	//Event Handlers
	//These are the methods that are called when certain SolidWorks events are fired
	STDMETHOD(OnDocChange)(void);
	STDMETHOD(OnModelDocChange)(void);
	STDMETHOD(OnDocLoad)(BSTR docTitle, BSTR docPath);
	STDMETHOD(OnFileNew)(LPDISPATCH newDoc, long docType, BSTR templateName);
	STDMETHOD(OnFileOpenPostNotify)(BSTR fileName);

	//Event Sinks
	//The SINK_MAP connects the specified SolidWorks event to a specific event handler
	BEGIN_SINK_MAP(CV5WrWorks)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppActiveDocChangeNotify, OnDocChange)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppDocumentLoadNotify, OnDocLoad)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppFileNewNotify2, OnFileNew)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppActiveModelDocChangeNotify, OnModelDocChange)
		SINK_ENTRY_EX(ID_SLDWORKS_EVENTS, __uuidof(DSldWorksEvents), swAppFileOpenPostNotify, OnFileOpenPostNotify)
	END_SINK_MAP( )

	// ISwAddin Methods
	//These are the methods inherited from the ISwAddin interface that 
	//need to be implemented in order to connect the addin to SolidWorks
public:
	STDMETHOD(ConnectToSW)(LPDISPATCH ThisSW, long Cookie, VARIANT_BOOL * IsConnected);
	STDMETHOD(DisconnectFromSW)(VARIANT_BOOL * IsDisconnected);

public:
	//UI Callbacks
	//These are the methds that are called by the addin created User Interface
	STDMETHOD(ToolbarCallback0)(void);
	STDMETHOD(ToolbarEnable0)(long* status);
	STDMETHOD(SaveV5Part)(BSTR FileName);
	STDMETHOD(SaveV5Product)(BSTR FileName);
#ifdef ParaSolid
	STDMETHOD(SaveV5PartPS)(BSTR FileName);
	STDMETHOD(SaveV5ProductPS)(BSTR FileName);
#endif
};

